//部分重排

var baseInfoService = new BaseInfo();
var curGradesAndClassesList = null;
var curConflictClassIdList = null;
var curCheckedPartUnscheduleClassIdList = [];

//设置部分排课
function setPartSchedule() {
    loadClassList();
}

//加载班级列表
function loadClassList() {
    $('#reschedule-settings-modal').modal('show');

    $("#reschedule-settings-modal-content").html("数据加载中...");

    if(curGradesAndClassesList != null){
        doLoadClassList()
    }else {
        baseInfoService.getGradesAndClassesList(taskId, function (gradesClassesList) {
            curGradesAndClassesList = gradesClassesList;
            getConflictClassList(function (conflictClassIdList) {
                curConflictClassIdList = conflictClassIdList;
                doLoadClassList();
            })
        })
    }
}

/**
 * 加载班级列表
 */
function doLoadClassList() {
    var htmlArr = [];
    for (var i = 0; i < curGradesAndClassesList.length; i++) {
        var gradesAndClasses = curGradesAndClassesList[i];
        var grade = gradesAndClasses.grade;
        var classes = gradesAndClasses.classes;

        htmlArr.push("<div style='font-weight: bold'>" + grade.gradeName + "</div>");
        htmlArr.push("<div style='padding-bottom: 20px'><ul>");
        for (var ii = 0; ii < classes.length; ii++) {
            var gradeClass = classes[ii];
            var classId = gradeClass.classId;
            var className = gradeClass.className;
            var disabled = ""
            // 有冲突的班级，不能设置不排课
            if (curConflictClassIdList.contains(classId)) {
                disabled = "disabled='disabled'"
            }

            var checked = "";
            if (curCheckedPartUnscheduleClassIdList.contains(classId)) {
                checked = "checked='checked'";
            }

            htmlArr.push("<li style='font-size: 13px;'>" +
                "<input type='checkbox' name='classId' value='" + classId + "' " + disabled + " " + checked + "> "
                + grade.gradeName + className + "</li>");
        }
        htmlArr.push("</ul><br style='clear: both'></div>");
    }

    $("#reschedule-settings-modal-content").html(htmlArr.join(""));
}

//获取有课程冲突的班级id
function getConflictClassList(callback) {
    var classes = null
    jQuery.ajax({
        url: "/m/conflict/classes?taskId=" + taskId,
        cache: false,
        success: function (ret) {
            if (ret != "") {
                classes = JSON.parse(ret);
            }
        },
        complete: function () {
            callback(classes);
        }
    });
}

function savePartRescheduleSettings() {
    curCheckedPartUnscheduleClassIdList = getUnScheduleClassIdList();
    setPartUnscheduleClassCount();
}

//获取不需要排课的班级Id
function getUnScheduleClassIdList() {
    var classIdList = [];
    $("#reschedule-settings-modal-content input[name='classId']").each(function () {
        var $the = $(this);
        if ($the.is(":checked")) {
            classIdList.push($the.val());
        }
    });

    return classIdList;
}

/**
 * 设置不重排的班级数量
 */
function setPartUnscheduleClassCount() {
    // 设置不重排的数量
    var partUnscheduleClassCount = getPartUnscheduleClassCount();
    $("#partScheduleCountMsg").html(partUnscheduleClassCount)
}

/**
 * 获取不重排的班级数量
 * @returns {Number}
 */
function getPartUnscheduleClassCount() {
    var partUnscheduleClassCount = getPartUnscheduleClassCount();
    return partUnscheduleClassCount;
}

/**
 * 获取不重排的班级数量
 * @returns {Number}
 */
function getPartUnscheduleClassCount() {
    var partUnscheduleClassCount = curCheckedPartUnscheduleClassIdList.length;
    return partUnscheduleClassCount;
}

function resetPartShedule() {
    curGradesAndClassesList = null;
    curConflictClassIdList = null;
}